package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 首页显示商品信息
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @PostMapping("/listInventory")
    public Map<String,Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){
        System.out.println(goodsTypeId);
        List<Goods> list = goodsService.selectPage(page,rows,codeOrName,goodsTypeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("rows",list);
        return map;
    }

    /**
     * 商品管理分页显示
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @PostMapping("/list")
    public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        List<Goods> goods = goodsService.selectPage(page, rows, goodsName, goodsTypeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",goods.size());
        map.put("rows",goods);
        return map;
    }

    /**
     * 新增和修改商品
     * @param goods
     * @param request
     * @return
     */
    @PostMapping("save")
    public ServiceVO saveOrUpdate(Goods goods, HttpServletRequest request){
        String goodsId1 = request.getParameter("goodsId");
        Boolean flag = goodsService.savaOrUpdate(goods,goodsId1);
        if (flag){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }
        return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.REQ_ERROR_MESS);
    }

    /**
     * 根据id删除商品
     * @param goodsId
     * @return
     */
    @PostMapping("delete")
    public ServiceVO deleteGoodsById(Integer goodsId){
        Boolean flag = goodsService.deleteGoods(goodsId);
        if (flag){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }
        return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.REQ_ERROR_MESS);
    }

    /**
     * 查询没有库存的商品
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        List<Goods> list = goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("rows",list);
        return map;
    }

    /**
     * 查询有库存的商品信息
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @PostMapping("getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        List<Goods> list = goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("rows",list);
        return map;
    }

    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */
    @PostMapping("saveStock")
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
        Boolean flag = goodsService.saveStock(goodsId, inventoryQuantity, purchasingPrice);
        if (flag){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }
        return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.REQ_ERROR_MESS);
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @PostMapping("deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
        Boolean flag = goodsService.deleteStock(goodsId);
        if (flag){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }
        return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.REQ_ERROR_MESS);
    }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     * @return
     */
    @PostMapping("listAlarm")
    public Map<String,Object> listAlarm(){
        List<Goods> list = goodsService.selectListAlarm();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }
}
